package com.example.urltovoice.controller;

import com.example.urltovoice.pojo.Url;
import com.example.urltovoice.service.DatabaseService;
import com.example.urltovoice.service.ICrawlService;
import com.example.urltovoice.service.ITextToVoice;
import com.example.urltovoice.service.impl.CsdnCrawlService;
import com.example.urltovoice.service.impl.WinTextToVoice;
import com.example.urltovoice.service.impl.WxCrawlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.sql.SQLException;
@Controller
@RequestMapping("UTVC")
public class UrlToVoiceController {
    @Autowired
    DatabaseService databaseService;
    ICrawlService crawlService;
    ITextToVoice textToVoice;
    @ResponseBody
    @GetMapping("UTVC")
    public Url crawling(@RequestParam String url) throws IOException, SQLException {
        Url Url = new Url();
        if(databaseService.SelectDatabase(url)!=null){
            System.out.println(databaseService.SelectDatabase(url));
            Url=databaseService.SelectDatabase(url);
        }else{
            String[] str = url.split("\\.");
            if(str.length<2){
                return null;
            }
            switch(str[1]+str[2]){
                case "weixinqq":
                    crawlService=new WxCrawlService();
                    break;
                case "blogcsdn":
                    crawlService=new CsdnCrawlService();
                    break;
                default:
                    return null;
            }
            Url.setUrl(url);
            Url.setText(crawlService.crawlService(url));
            if(Url.getText()==null){
                return null;
            }else{
                textToVoice=new WinTextToVoice();
                Url.setSrc(textToVoice.TextToVoice(Url.getText()));
                databaseService.InsertDatabase(Url);
            }
        }
        return Url;
    }

}
